<?php
require_once ("secure_area.php");
require_once ("interfaces/idata_controller.php");
class Materials extends Secure_area implements iData_controller
{
	function __construct()
	{
		parent::__construct('materials');
	}

	function index()
	{
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=get_materials_manage_table($this->Material->get_all(),$this);
		$this->load->view('materials/manage',$data);
	}

	function refresh()
	{
		$low_inventory=$this->input->post('low_inventory');
		$is_serialized=$this->input->post('is_serialized');
		$no_description=$this->input->post('no_description');

		$data['search_section_state']=$this->input->post('search_section_state');
		$data['low_inventory']=$this->input->post('low_inventory');
		$data['is_serialized']=$this->input->post('is_serialized');
		$data['no_description']=$this->input->post('no_description');
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=get_materials_manage_table($this->Item->get_all_filtered($low_inventory,$is_serialized,$no_description),$this);
		$this->load->view('materials/manage',$data);
	}

//	function find_item_info()
//	{
//		$item_number=$this->input->post('scan_item_number');
//		echo json_encode($this->Item->find_item_info($item_number));
//	}

	function search()
	{
		$search=$this->input->post('search');
		$data_rows=get_materials_manage_table_data_rows($this->Material->search($search),$this);
		echo $data_rows;
	}

	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Material->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}

	function get_row()
	{
		$material_id = $this->input->post('row_id');
		$data_row=get_material_data_row($this->Material->get_info($material_id),$this);
		echo $data_row;
	}

	function view($material_id=-1)
	{
		$data['material_info']=$this->Material->get_info($material_id);
		
		$this->load->view("materials/form",$data);
	}
	
	function save($material_id=-1)
	{
		$material_data = array(
		'type'=>$this->input->post('type'),
		'weight_per_unit'=>$this->input->post('weight_per_unit'),
		'price'=>$this->input->post('price'),
		'price_per_unit'=>$this->input->post('price_per_unit'),
		'timestamp'=>date('Y-m-d H:i:s')
		);
		
		$employee_id=$this->Employee->get_logged_in_employee_info()->person_id;
		$cur_material_info = $this->Material->get_info($material_id);


		if($this->Material->save($material_data, $material_id))
		{
			//New material
			if($material_id==-1)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('materials_successful_adding').' '.
				$material_data['type'],'material_id'=>$material_data['material_id']));
				$material_id = $material_data['material_id'];
			}
			else //previous material
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('materials_successful_updating').' '.
				$material_data['type'],'material_id'=>$material_id));
			}
			
		}
		else//failure
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('materials_error_adding_updating').' '.
			$material_data['type'],'material_id'=>-1));
		}

	}
	
	function delete()
	{
		$materials_to_delete=$this->input->post('ids');

		if($this->Material->delete_list($materials_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('materials_successful_deleted').' '.
			count($materials_to_delete).' '.$this->lang->line('materials_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('materials_cannot_be_deleted')));
		}
	}

	/**
	 * Display form: Import data from an excel file
	 * @author: Nguyen OJB
	 * @since: 10.1
	 */
	function excel_import()
	{
		$this->load->view("materials/excel_import", null);
	}

	/**
	 * Read data from excel file -> save it to databse
	 * @author: Nguyen OJB
	 * @since: 10.1
	 */
	function do_excel_import()
	{
		$msg = "do_excel_import";
		$failCodes = array();
		if ($_FILES['file_path']['error']!=UPLOAD_ERR_OK)
		{
			$msg = $this->lang->line('materials_excel_import_failed');
			echo json_encode( array('success'=>false,'message'=>$msg) );
			return ;
		}
		else
		{
			$this->load->library('spreadsheetexcelreader');
			$this->spreadsheetexcelreader->store_extended_info = false;
			$success = $this->spreadsheetexcelreader->read($_FILES['file_path']['tmp_name']);

			$rowCount = $this->spreadsheetexcelreader->rowcount(0);
			if($rowCount > 2){
				$successCount = 0;
				for($i = 3; $i<=$rowCount; $i++){
					$material_data = array(
						'type'			=>	$this->spreadsheetexcelreader->val($i, 'A'),
						'weight_per_unit'	=>	$this->spreadsheetexcelreader->val($i, 'B'),
						'price'		=>	$this->spreadsheetexcelreader->val($i, 'C'),
						'price_per_unit'	=>	$this->spreadsheetexcelreader->val($i, 'D'),
						'timestamp' => date('Y-m-d H:i:s')
					);

					$tmp_type1 = $this->spreadsheetexcelreader->type($i, 'B');	
					$tmp_type2 = $this->spreadsheetexcelreader->type($i, 'C');	
					
					$tmp_val = $this->spreadsheetexcelreader->val($i, 'C');	
					if( ! $this->Material->save($material_data)) {
						//insert or update item failure
						$failCodes[] = $i;
					}
					$successCount++;
				}
			} else {
				// rowCount < 2
				echo json_encode( array('success'=>true,'message'=>'Your upload file has no data or not in supported format.') );
				return;
			}
		}

		$success = true;
		if(count($failCodes) > 1){
			$msg = "Most materials imported. But some were not, here is list of their CODE (" .count($failCodes) ."): ".implode(", ", $failCodes);
			$success = false;
		}else{
			$msg = "Import ".$successCount." material(s) successful";
		}

		echo json_encode( array('success'=>$success,'message'=>$msg) );
	}

	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{
		return 720;
	}
}
?>